package com.pxl.springbootinit.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxl.springbootinit.common.BaseResponse;
import com.pxl.springbootinit.common.ErrorCode;
import com.pxl.springbootinit.exception.BusinessException;
import com.pxl.springbootinit.model.dto.item.ItemAuditForm;
import com.pxl.springbootinit.model.entity.Item;
import com.pxl.springbootinit.model.entity.ItemAudit;
import com.pxl.springbootinit.model.enums.ReviewStatusEnum;
import com.pxl.springbootinit.service.ItemAuditService;
import com.pxl.springbootinit.mapper.ItemAuditMapper;
import com.pxl.springbootinit.service.ItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author MADAO
* @description 针对表【item_audit(物品审核记录表)】的数据库操作Service实现
* @createDate 2024-12-20 12:02:23
*/
@Service
public class ItemAuditServiceImpl extends ServiceImpl<ItemAuditMapper, ItemAudit>
    implements ItemAuditService{

    @Resource
    private ItemService itemService;

    /**
     * 管理员审核商品状态
     * @param itemAuditForm
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean pass(ItemAuditForm itemAuditForm) {
        Item item = itemService.getById(itemAuditForm.getItemId());
        if (item == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"物品不存在");
        }
        ItemAudit itemAudit = new ItemAudit();
        BeanUtils.copyProperties(itemAuditForm, itemAudit);
        item.setStatus(ReviewStatusEnum.APPROVED.getValue());
        itemAudit.setAuditStatus(ReviewStatusEnum.APPROVED.getValue());
        if (!(this.save(itemAudit) && itemService.updateById(item))){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"审核失败");
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean reject(ItemAuditForm itemAuditForm) {
        Item item = itemService.getById(itemAuditForm.getItemId());
        if (item == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"物品不存在");
        }
        ItemAudit itemAudit = new ItemAudit();
        BeanUtils.copyProperties(itemAuditForm, itemAudit);
        item.setStatus(ReviewStatusEnum.REJECTED.getValue());
        itemAudit.setAuditStatus(ReviewStatusEnum.REJECTED.getValue());
        if (!(this.save(itemAudit) && itemService.updateById(item))){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"审核失败");
        }
        return true;
    }
}




